﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T13DelegateAndEvent
{
    
    //事件的发布者
    class EventBuy
    {
       //定义委托
       public delegate void BuyEventHandler();
      //定义事件对象
      public event BuyEventHandler OnBuy;

        //事件的触发
        public void Issue()
        {
            if(OnBuy!=null)
            {
                Console.WriteLine("水果到了!");
                //触发事件
                OnBuy();
            }
        }
    }
    //事件的订阅者
    class MrHu
    {
        public void Receive()
        {
            Console.WriteLine("张三收到了消息，要买苹果");
        }
        //注册事件
        public void Register(EventBuy e)
        {
            e.OnBuy += Receive;
            //e.OnBuy -= Receive;
            //e.OnBuy = Receive;//事件不允许直接赋值操作
        }
    }
    class MrXu
    {
        public void Receive()
        {
            Console.WriteLine("李师师收到了消息，要买橙子");
        }
        //注册事件
        public void Register(EventBuy e)
        {
            e.OnBuy += Receive;
            //e.OnBuy -= Receive;
            //e.OnBuy = Receive;//事件不允许直接赋值操作
        }
    }
}
